<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    {include file="common/head" /}
</head>

<body>
{include file="common/bread" /}
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <form class="layui-form layui-col-space5" action="" method="get">

                        <div class="layui-input-inline layui-show-xs-block">
                            <input type="text" id="title" name="title" value="{$gd.title|default=''}" placeholder="服务名称" autocomplete="off" class="layui-input">
                        </div>

                        <div class="layui-input-inline layui-show-xs-block">
                            <button class="layui-btn" type="submit">
                                <i class="layui-icon">&#xe615;</i>
                                搜索
                            </button>
                        </div>
                    </form>
                </div>

                <div class="layui-card-header">
                    <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
                    <button class="layui-btn" onclick="xadmin.open('编辑','{:url("edit")}')"><i class="layui-icon">&#xe642;</i>增加服务</button>
                </div>
                <div class="layui-card-body ">

                    <!--每个tr 上有两个属性 cate-id='1' 当前分类id fid='0' 父级id ,顶级分类为 0，有子分类的前面加收缩图标<i class="layui-icon x-show" status='true'>&#xe623;</i>-->
                    <table class="layui-table layui-form">
                        <thead>
                        <tr>
                            <th width="20">
                                <input type="checkbox" id="c_all" name="" lay-skin="primary" lay-filter="all">
                            </th>
                            <th width="70">ID</th>
                            <th>服务名称</th>
                            <th>首页图片</th>
                            <th>服务图片</th>
                            <th>服务费用(元)</th>
                            <th>服务电话</th>
                            <th>状态</th>
                            <th width="250">操作</th>
                        </thead>
                        <tbody class="x-cate">
                        {volist name="list" id="vo"}
                        <tr  >
                            <td>
                                <input type="checkbox" class="ids" name="ids" value="{$vo.id}" lay-skin="primary" lay-filter="child">
                            </td>
                            <td>{$vo.id}</td>
                            <td>{$vo.title}</td>
                            <td>
                                <img src="{$vo['thumb_min']}" style="width: 100px; height: 50px;" alt="">
                            </td>
                            <td>
                                <img src="{$vo['thumb']}" style="width: 100px; height: 50px;" alt="">
                            </td>
                            <td>
                                {$vo['price']}
                            </td>
                            <td>
                                {$vo['mobile']}
                            </td>
                            <td>
                                <input type="checkbox" name="{$vo['id']}" value="{$vo['id']}" lay-text="正常|禁用" {eq name="vo['status']" value="1"}checked=""{/eq} lay-skin="switch">
                            </td>
                            <td class="td-manage">
                                <button class="layui-btn layui-btn layui-btn-xs"  onclick="xadmin.open('编辑','{:url("edit",["id"=>$vo["id"],'model'=>'car'])}')" ><i class="layui-icon">&#xe642;</i>编辑/查看</button>
                                <button class="layui-btn-danger layui-btn layui-btn-xs"  onclick="member_del(this,'{$vo["id"]}')" href="javascript:;" ><i class="layui-icon">&#xe640;</i>删除</button>
                            </td>
                        </tr>

                        {/volist}

                        </tbody>
                    </table>
                </div>
                <div class="layui-card-body ">
                    {$list|raw}
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form'], function(){
        form = layui.form;

        form.on('switch', function(data){

            $.ajax({
                type: "POST",
                url: "{:url('acStatus')}",
                data: {id:data.value,status:data.elem.checked?1:0},
                dataType:'json',
                success: function (result) {
                    console.log(result);
                    if(result.code){
                        layer.msg(result.msg,{icon:1,time:1000});
                    }else{
                        layer.msg(result.msg,{icon:2,time:1000});
                    }
                },
                error: function(data) {
                    layer.msg(data.msg,{icon:2,time:1000});
                }

            });

        });

        //全选
        form.on('checkbox(all)', function (data) {
            $(".ids").prop("checked", data.elem.checked);
            form.render('checkbox');
        });
        //有一个未选中全选取消选中
        form.on('checkbox(child)', function (data) {
            var item = $(".ids");
            for (var i = 0; i < item.length; i++) {
                if (item[i].checked == false) {
                    $("#c_all").prop("checked", false);
                    form.render('checkbox');
                    break;
                }
            }
            //如果都勾选了  勾上全选
            var  all=item.length;
            for (var i = 0; i < item.length; i++) {
                if (item[i].checked == true) {
                    all--;
                }
            }
            if(all==0){
                $("#c_all").prop("checked", true);
                form.render('checkbox');}
        });

    });

    /*用户-删除*/
    function member_del(obj,id){
        layer.confirm('确认要删除吗？',function(index){
            //发异步删除数据
            $.ajax({
                type: "POST",
                url: "{:url('del')}",
                data: {id:id},
                dataType:'json',
                success: function (result) {
                    if(result.code){
                        $(obj).parents("tr").remove();
                        layer.msg(result.msg,{icon:1,time:1000});
                    }else{
                        layer.msg(result.msg,{icon:2,time:1000});
                    }
                },
                error: function(data) {
                    layer.msg(data.msg,{icon:2,time:1000});
                }

            });


        });
    }


    function delAll() {
        var item=$("input[name='ids']:checked");
        var ids=[];
        $.each(item,function (vo,key) {
            ids.push($(this).val())
        });

        if(ids.length<=0){
            layer.alert("请选择要删除的项！", {
                icon: 2
            });
            return false;
        }

        $.ajax({
            type: "POST",
            url: "{:url('del')}",
            data: {id:ids,type:1},
            dataType:'json',
            success: function (result) {
                if(result.code){
                    layer.msg(result.msg, {
                        icon: 1,
                        time: 1000
                    },function () {
                        location.reload()
                    });
                }else{
                    layer.msg(result.msg,{icon:2,time:1000});
                }
            },
            error: function(data) {
                layer.msg(data.msg,{icon:2,time:1000});
            }

        });
    }
</script>
</body>
</html>
